#include "UnderwriterInfo.h"

#include <string.h>

CUnderwriterInfo::CUnderwriterInfo(void) {}

CUnderwriterInfo::~CUnderwriterInfo(void) { Clear(); }

void CUnderwriterInfo::Clear() {
  std::lock_guard<std::mutex> locker(m_mutex);
  m_map.clear();
}

bool CUnderwriterInfo::Add(QBUNDERWRITERINFO& info) {
  // TODO: std::lock_guard<std::mutex> locker(m_mutex);
  if (strlen(info.m_UnderwriterUUID) <= 0) return false;
  m_map[info.m_UnderwriterUUID] = info;
  return true;
}

const char* CUnderwriterInfo::GetUnderwriterCode(const char* sUUID) {
  if (m_map.find(sUUID) != m_map.end()) {
    return m_map[sUUID].m_UnderwriterCode;
  }
  return nullptr;
}

const char* CUnderwriterInfo::GetUnderwriterUUID(const char* sCode) {
  for (std::map<std::string, QBUNDERWRITERINFO>::iterator it = m_map.begin();
       it != m_map.end(); it++) {
    if (strcmp(it->second.m_UnderwriterCode, sCode) == 0)
      return it->second.m_UnderwriterUUID;
  }
  return nullptr;
}

const QBUNDERWRITERINFO* CUnderwriterInfo::GetUnderwriterByUUID(
    const char* sUUID) {
  for (std::map<std::string, QBUNDERWRITERINFO>::iterator it = m_map.begin();
       it != m_map.end(); it++) {
    if (strcmp(it->second.m_UnderwriterUUID, sUUID) == 0) return &it->second;
  }
  return nullptr;
}

void CUnderwriterInfo::DumpStatus(int mode) const {}

CUnderwriterInfo& CUnderwriterInfo::instance() {
  static CUnderwriterInfo s_cwi;
  return s_cwi;
}
